package com.kawaiior.asoulworld.items.weapon;

import com.kawaiior.asoulworld.capability.CapabilityRegistryHandler;
import com.kawaiior.asoulworld.capability.MiyuPower;
import com.kawaiior.asoulworld.items.AsWeaponBase;
import com.kawaiior.asoulworld.util.EntityUtil;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.MobEffects;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.world.World;

import javax.annotation.Nonnull;

import static com.kawaiior.asoulworld.events.ASEventHelper.teleportToEntity;
import static com.kawaiior.asoulworld.util.EntityUtil.getPlayerLookTarget;

public class ItemMiyuWand extends AsWeaponBase {

    public ItemMiyuWand(String name) {
        super(name);
        this.max_damage = 0;
    }

    @Nonnull
    @Override
    public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, @Nonnull EnumHand hand) {
        if (world.isRemote) return super.onItemRightClick(world,player,hand);
        ItemStack stack = player.getHeldItem(hand);
        MiyuPower power = stack.getCapability(CapabilityRegistryHandler.MIYU_POWER, null);
        if (power == null) return super.onItemRightClick(world, player, hand);

        if (power.isBack()){
            player.setPositionAndUpdate(power.getPosX(), power.getPosY(), power.getPosZ());
            power.setBack(false);
            world.createExplosion(player, power.getPosX(), power.getPosY(), power.getPosZ(),3,false);
            player.getCooldownTracker().setCooldown(this,100);
            return super.onItemRightClick(world,player,hand);
        }

        Entity pointedEntity = getPlayerLookTarget(world, player, 128D);

        if (pointedEntity instanceof  EntityLivingBase){
            power.setPos(player.posX, player.posY, player.posZ);
            power.setBack(true);
            power.setMoveTime(System.currentTimeMillis()+10000L);

            EntityUtil.ApplyBuff((EntityLivingBase) pointedEntity, MobEffects.SLOWNESS,1,10f);
            EntityUtil.ApplyBuff(player, MobEffects.STRENGTH, 1, 5f);

            teleportToEntity(player, pointedEntity);
        }

        return super.onItemRightClick(world, player, hand);
    }

    @Override
    public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
        if (worldIn.getTotalWorldTime() % 20 != 0 || worldIn.isRemote || !(entityIn instanceof EntityPlayer)) return;
        EntityPlayer player = (EntityPlayer)entityIn;
        MiyuPower power = stack.getCapability(CapabilityRegistryHandler.MIYU_POWER, null);
        if (power != null && power.isBack()){
            if (power.getMoveTime() <= System.currentTimeMillis()){
                power.setBack(false);
                System.out.println(power.isBack());
                player.getCooldownTracker().setCooldown(this,100);
            }
        }
    }

    @Override
    public int getMaxItemUseDuration(ItemStack stack)
    {
        return 10;
    }

    @Override
    public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft) {

    }

}
